package com.yanq.liancar.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.ExchangeVoucherAdapter;
import com.yanq.liancar.bean.ExchangeVoucherBean;
import com.yanq.liancar.bean.QueryUserBaseMessageBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallCExchangeVoucherActivity extends BaseToolbarActivity {
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.et_Exchangenum)
    EditText etExchangenum;
    @BindView(R.id.tv_Exchange)
    TextView tvExchange;
    @BindView(R.id.exchangevoucher_recyclerview)
    RecyclerView orderRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_History)
    LinearLayout ll_History;
    private int page=1;
    private boolean isClear=true;
    private ExchangeVoucherAdapter adapter;
    private List<ExchangeVoucherBean.DataBean> mList=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, true,false);
        setContentView(R.layout.activity_smallc_exchangevoucher);
        ButterKnife.bind(this);
        mTvTitle.setText("我的兑换券");

        initRecyclerview();
        initListener();

        initOkGoWhole();

    }

    @OnClick(R.id.tv_Exchange)
    public void onViewClicked() {
        //小键盘隐藏于切换
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        if (!TextUtils.isEmpty(etExchangenum.getText().toString().trim())){
            initOkGoWhole1();
        }else {
            RxToast.error("请输入兑换码");
        }
    }


    private void initRecyclerview() {
        orderRecyclerview.setNestedScrollingEnabled(false);
        orderRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ExchangeVoucherAdapter(R.layout.item_exchangevoucher, mList);
        orderRecyclerview.setAdapter(adapter);

    }

    private void initListener() {
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;


                refreshlayout.finishRefresh();
            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;

                refreshLayout.finishLoadMore();
            }

        });
    }


    //查询
    private void initOkGoWhole() {
        OkGo.<String>post(Config.s + "llc/api/selectExchangeRecord")
                .tag(this)
                .params("token", UserBean.getcToken(mContext))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ExchangeVoucherBean bean = gson.fromJson(response.body(), ExchangeVoucherBean.class);
                        if (bean.isIsSuccess()) {
                            if (isClear){
                                mList.clear();
                            }
                            mList.addAll(bean.getData());
                            adapter.setNewData(mList);
                            if (mList.size()<1){
                                ll_History.setVisibility(View.GONE);
                            }
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }
                });
    }

    //兑换
    private void initOkGoWhole1() {
        OkGo.<String>post(Config.s + "llc/api/ExchangeVouchers")
                .tag(this)
                .params("token", UserBean.getcToken(mContext))
                .params("code", etExchangenum.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        QueryUserBaseMessageBean bean = gson.fromJson(response.body(), QueryUserBaseMessageBean.class);
                        if (bean.isIsSuccess()) {
                            etExchangenum.setText("");
                            RxToast.success("兑换成功");
                            initOkGoWhole();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }
                });
    }


}
